<?php
namespace Develop\Controller;
use Common\Controller\AdminbaseController;
class MenuController extends AdminbaseController 
{   
    protected $menuObj;
	public function _initialize()
	{
        parent::_initialize();
        $this->menuObj = D('Menu');
	}

	public function index()
	{
			$this->lists();
	}


    public function lists()
    {
        $menuTree =$this->tree(0,0);
        $this->assign("list",$menuTree);
        $this->display('list');
    }

    public function tree($parentid=0,$level=0,$html='|── ')
    {
        static $tree = array();
        $menuList = $this->getMenuList($parentid);
        if (!empty($menuList)) 
        {
            foreach($menuList as $menu)
            {
                $menu['sort'] = $level;
                $html=str_repeat($html,$level);
                $menu['html'] = $html;
                $tree[] = $menu;
                $parentId = $menu['id'];
                $this->tree($parentId,$level+1);
            }
        }
        return $tree;
    }

    public function getMenuList($parentId)
    {
        $menuList = $this->menuObj->where(array("parentid" => $parentId))->order(array("listorder" => "ASC"))->select();
        return $menuList;
    }

	public function getParentMenu($type=1)
	{
        $parentMenuObjs = $this->menuObj->cache(true, 60)->where(array("parentid"=>0,"status"=> 1,"type"=>$type))->select();
        return $parentMenuObjs;
	}


	public function getChildrenMenu($parentId) 
    {
        $childrenMenuObjs =  $this->menuObj->where(array("parentid"=>$parentId,"status"=> 1))->select();
        return $childrenMenuObjs;
    }

    public function ajaxGetChildrenMenu($parentId) 
    {
        $childrenMenuObjs = $this->menuObj->where(array("parentid"=>$parentId))->select();
        if (!empty($childrenMenuObjs)) 
        {
            $firstId = $childrenMenuObjs[0]['id'];
            $arr = array("status"=>'1',"childrenMenuObjs"=>$childrenMenuObjs,"firstId"=>$firstId);
        }
        else
        {
            $arr = array("status"=>'0',"message"=>"暂无数据");
        }
        echo json_encode($arr);    
    }

    public function add()
    {
        $parentId = $_GET['parentid'];
    	$parentMenuObj =  $this->getParentMenuName($parentId);
    	$this->assign("parentMenuObj",$parentMenuObj);
    	$this->display("add");
    }
    public function getParentMenuName($parentId=0)
    {
        $parentMenuObj=null;
        if ($parentId>0) 
        {
          $parentMenuObj = $this->menuObj->find($parentId);
          if (!empty($parentMenuObj)&&$parentMenuObj['parentid']!=0) 
          {
             $parentId= $parentMenuObjs['parentid'];
             $topparentMenuObj = $this->menuObj->find($parentId);
             $parentMenuObj['name']=$topparentMenuObj['name']." >> ".$parentMenuObj['name'];
          }
        }
        return $parentMenuObj;
    }

    public function edit($id=0)
    {
    	$menuObj=null;
    	if ($id>0) 
    	{
	    	$menuObj =  $this->menuObj->find($id);
            $parentId = $menuObj['parentid'];
            $parentMenuObj =  $this->getParentMenuName($parentId);
            $this->assign("parentMenuObj",$parentMenuObj);
    	}
    	$this->assign("menuObj",$menuObj);
    	$this->display("edit");
    }
    public function save()
    {
        $id = I('post.inputId','','htmlspecialchars');
        $parentId = I('post.inputParentId','','htmlspecialchars');
        $name = I('post.inputName','','htmlspecialchars');
        $model = I('post.inputModel','','htmlspecialchars');
        $controller = I('post.inputController','','htmlspecialchars');
        $action = I('post.inputAction','','htmlspecialchars');
        $icon = I('post.inputIcon','','htmlspecialchars');
        $type = I('post.inputType','','htmlspecialchars');
        $status = I('post.inputStatus','0','htmlspecialchars');
        $data = array();
        $data["parentid"]  = $parentId;
        $data["name"]  = $name;
        $data["model"] = $model;
        $data["controller"] = $controller;
        $data["action"] = $action;
        $data["icon"]    = $icon;
        $data["type"]    = $type;
        $data["status"]    = $status;
        if($id>0)
        {
            $editResult = $this->menuObj->where("id=%d",$id)->save($data);
            if($editResult) 
            {
              $arr = array("status"=>'1',"message"=>"编辑菜单成功","url"=>"index");
            }else
            {
              $arr = array("status"=>'0',"message"=>"编辑博客失败");
            }
        }
        else
        {
            $addResult =   $this->menuObj->add($data);
            if($addResult) {
                $arr = array("status"=>'1',"message"=>"添加菜单成功","url"=>"index");
            }else{
                $arr = array("status"=>'0',"message"=>"添加菜单失败");
            }
        }
        echo json_encode($arr);
    }

	/*排序更新*/
    public function listorder()
    {
       $ids = I('post.ids','-1','htmlspecialchars');
       $listorders = I('post.listorders','','htmlspecialchars');  
       parent::_listorder($this->menuObj,$ids,$listorders);
    }
    /*单个删除 id*/
    public function delete($id)
    {
        parent::_delete($this->menuObj,$id);
    }

    /* 批量删除 ids*/
    public function checkDeletes($ids)
    {
      parent::_checkDeletes($this->menuObj,$ids);
    }
    /* 单个修改当前状态status 0 1 -1*/
    public function changeStatus($id,$status)
    {
        parent::_changeStatus($this->menuObj,$id,$status);
    }
    /*批量 修改当前状态status 0 1 -1*/
    public function checkChanges($status,$ids)
    {
      parent::_checkChanges($this->menuObj,$status,$ids);
    }
}